package com.probiz.estore.system.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.probiz.estore.common.model.system.PaymentMethod;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.system.dao.PaymentMethodDao;

/**
 * PaymentMethod Data Access Object (DAO) implementation. Developer introduced interfaces should be
 * declared here. Won't get overwritten.
 */
@SuppressWarnings({ "serial", "unchecked" })
public class PaymentMethodDaoImpl extends HibernateGenericDaoImpl<PaymentMethod> implements PaymentMethodDao {
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.dao.PaymentMethodDao#updatePaymentMethodStatus(java.lang.Integer, java.lang.Short)
	 */
	public void updatePaymentMethodStatus(Integer paymentMethodId, Short status) {

		PaymentMethod paymentMethod = this.getById(paymentMethodId);
		paymentMethod.setPaymentMethodId(paymentMethodId);
		paymentMethod.setStatus(status);

		save(paymentMethod);

	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.system.dao.PaymentMethodDao#findAllByStatus(java.lang.String)
	 */
	public List<PaymentMethod> findAllByStatus(String statusArr) {
		return findByHql(" from PaymentMethod vo where vo.status in " + statusArr+" order by vo.paymentMethodId");
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.dao.PaymentMethodDao#getByPaymentMethodType(java.lang.Short)
	 */
	public List<PaymentMethod> getByPaymentMethodType(Short paymentMethodType) {
		return findByHql(" from PaymentMethod vo where vo.paymentMethodType=?", paymentMethodType);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.dao.PaymentMethodDao#getByPaymentMethodTypeStatus(java.lang.Short, java.lang.Short)
	 */
	public List<PaymentMethod> getByPaymentMethodTypeStatus(Short paymentType, Short status) {
		return findByHql(" from PaymentMethod vo where vo.paymentMethodType=? and vo.status=?", new Object[] { paymentType, status });
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.dao.PaymentMethodDao#getMaxSortOrder()
	 */
	public Integer getMaxSortOrder() {

		String hql = "select max(sortOrder) from PaymentMethod";
		Query query = getSession().createQuery(hql);
		Object obj = query.uniqueResult();
		Integer maxOrder = Integer.valueOf(0);
		if (obj != null) {
			maxOrder = (Integer) obj;
		}
		return maxOrder;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.dao.PaymentMethodDao#getByPaymentGatewayId(java.lang.Integer)
	 */
	public PaymentMethod getByPaymentGatewayId(Integer paymentGatewayId) {
		String hql = "from PaymentMethod pm where pm.paymentGateway.paymentGatewayId=?";
		Object obj = findUnique(hql, paymentGatewayId);
		if (obj != null) {
			return (PaymentMethod) obj;
		} else {
			return null;
		}
	}
	public List<PaymentMethod> getOnlinePaymentMethodByPaymentGatewayTypeStatus(Short paymentGatewayType, Short status) {
		return findByHql(" from PaymentMethod pm where pm.paymentGateway.paymentGatewayType = ? and pm.status = ?", new Object[] { paymentGatewayType, status });
	}
}